package net.pinnoocle.playservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.pinnoocle.playdao.dao.UserLevelDao;
import net.pinnoocle.playdao.entity.UserLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户等级(UserLevel)表服务实现类
 *
 * @author ye
 * @since 2021-10-25 15:00:15
 */
@Service("userLevelService")
public class UserLevelServiceImpl extends ServiceImpl<UserLevelDao, UserLevel> {

    @Autowired
    UserServiceImpl userService;

    /**
     * 通过用户玩本次数获取会员级别
     * @param playTimes
     * @return
     */
    public int getMemberLevelId(Integer playTimes){
        String shopId = userService.getGlobalShopId();
        int memberLevelId = 0;
        List<UserLevel> list = this.list(new QueryWrapper<UserLevel>()
                .lambda()
                .eq(UserLevel::getShopId, shopId)
                .orderByDesc(UserLevel::getAutoUpgrade));
        if(CollectionUtils.isEmpty(list)){
            return memberLevelId;
        }
        for (UserLevel item : list){
            if(playTimes >= item.getAutoUpgrade()){
                memberLevelId = item.getLevelId();
                break;
            }
        }
        return memberLevelId;
    }
}